<?php slot('studyGroupsNav', 'active')?>
<div class="courses" id="wrapper">
	<?php include_partial('global/systemMessages')?>
	<div id="content">
		<div id="main">
			<div class="main-heading">
				<h1><?php echo $group->getName() ?></h1>
			</div>
			<?php include_partial('groups/subnav', array('group_id' => $group->getId(), 'current' => 'Tasks')) ?>
			<div class="tasks-heading">
				<?php echo link_to('Create a New Task &#xBB;', 'tasks/new?group_id='.$group->getId())?>
				<?php echo link_to('Create a New Task List &#xBB;', 'task_lists/new?group_id='.$group->getId())?>
				<h2>Group Tasks</h2>
			</div>
			<?php if($task_lists != null): ?>
			<?php include_partial('calendar', array('calendar_data' => $calendar_data))?>				
			<form action="" id="group-tasks" class="tasks">
				<fieldset>
					<?php foreach($task_lists as $task_list): ?>
						<?php include_partial('task_lists/list', array('task_list' => $task_list, 'group' => $group))?>
					<?php endforeach ?>
				</fieldset>
			</form>
			<?php else: ?>
				<div class="non-member">
		            <h1>Group Tasks</h1>
					<p>This is the group task page.  Here you are able to create task lists and 
					assign tasks to group members for things like lecture notes and reading summaries.  
					Once there are some tasks, it will look similar to the sample below.</p>
				</div>
				<img alt="Sample view" src="/images/img_sample_groups-tasks.jpg"/>
			<?php endif ?>
		</div>
		<div id="sidebar">
			<?php include_component('group_members', 'list', array('group_id' => $group->getId())) ?>	
		</div>
		
	</div>
	<div id="loading-image" style="display: none;">
		<?php echo image_tag('ajax-loader_small.gif')?>
	</div>
</div>

<script type="text/javascript">

	/**
	* reload calendar
	*/
	function reloadCalendar(){

		
		$.get( "<?php echo (url_for('tasks/calendar').'/group_id/'.$group->getId())?>", {},
				function(data) {
					$('.milestones').replaceWith(data);
				}
		);
		return false;
	}

	/**
	* create confirm box for task list delete
	*/
	function deleteTaskListConfirm(id)
	{
		var name = $('#task_list_info_'+id+' > h3').html();
	
		
		var result = confirm("You will now delete the task list '"+name +"' ?");
		if (result == true)
	 	{	
		 	//redirect
			window.location.replace($('#taskListDeleteConfirm'+id).attr('href'));
	  	}
		else
	  	{
	  	}
	}

	/**
	* bind delete links
	*/
	function bindDeleteTaskList()
	{
		$('.task-list-delete').click(function(){
			return false;
		});
	}
	
	

	function editTaskList(task_list_id){
		$('#taskListEditForm'+task_list_id).html($('#loading-image').html());		// copy over the loading image
		$('#taskListEditForm'+task_list_id).show();
		
		$.get( $('#taskListEdit'+task_list_id).attr('href'), {},
				function(data) {
					$('#taskListEditForm'+task_list_id).html(data);

					var options = {
						type: 'post',
						success: function(resp, status) {
							//close form
							$('#taskListEditForm'+task_list_id).hide();
							//replace info with response
							$('#task_list_info_'+task_list_id).replaceWith(resp);
							// rebind all the links
							bindAddTask();
							bindEditTaskList();
							bindDeleteTaskList();
						}
					};
					
					$('#taskListForm'+task_list_id).submit(function(){
						$(this).ajaxSubmit(options);
						return false;
					});
				}
		);
		return false;
	}

	/**
	*  Display task list edit form when clicking on the link
	*/
	function bindEditTaskList() {
		$('.task-list-edit').click(function(){
			return false;
		});
	}

	function cancelEditTaskList(id) {
		$('#taskListEditForm' + id).hide();
	}

	function addTask(task_list_id){
		$('#taskListEditForm'+task_list_id).html($('#loading-image').html());		// copy over the loading image
		$('#taskListEditForm'+task_list_id).show();
		
		$.get( $('#taskAdd'+task_list_id).attr('href'), {},
				function(data) {
					$('#taskListEditForm'+task_list_id).html(data);

					var options = {
						type: 'post',
						success: function(resp, status) {
							//close form
							$('#taskListEditForm'+task_list_id).hide();
							//place response at the end of the task list
							$('#task_list_'+task_list_id + ' > .check-list').append(resp);
							// rebind all the links
							bindEditTask();
							bindChecklistActions();
							bindUpdateStatus();
							reloadCalendar();
						}
					};
					
					$('#newtaskbox'+task_list_id + ' > #taskForm').submit(function(){
						$(this).ajaxSubmit(options);
						return false;
					});
				}
		);
		return false;
	}

	/**
	*  Display task add for when clicking on the link
	*/
	function bindAddTask() {
		$('.task-add').click(function(){
			return false;
		});
	}
	
	function cancelAddTask(id) {
		$('#taskListEditForm' + id).hide();
	}
	

	function editTask(id) {
		$('#taskEditForm'+id).html($('#loading-image').html());		// copy over the loading image
		$('#taskEditForm'+id).show();
		$.get( $('#taskEdit'+id).attr('href'), {},
				function(data) {
					$('#taskEditForm'+id).html(data);

					var options = {
						type: 'post',
						success: function(resp, status) {
							//swap original element with response
							$('#task'+id).replaceWith(resp);
							// rebind all the links
							bindEditTask();
							bindChecklistActions();
							bindUpdateStatus();
							reloadCalendar();
						}
					};
					
					$('#taskForm'+id).submit(function(){
						$(this).ajaxSubmit(options);
						return false;
					});
				}
		);
		return false;
	}

	function assignTask(id) {
		$.get( $('#taskAssign'+id).attr('href'), {},
				function(data) {
					$('#task'+id).html(data);
					// rebind all teh links
					bindEditTask();
				}
		);
	}

	/**
	*  Display task edit form when clicking on the edit link
	*/
	function bindEditTask() {
		$('.task-edit').click(function(){
			return false;
		});
	}


	
	function cancelEditTask(id) {
		$('#taskEditForm' + id).hide();
	}


	/**
	*  Binds all task list items to mouse hover over event to display the check list actions
	*  of edit or volunteer
	*/
	function bindChecklistActions() {
		// dynamically display check list actions for each task
		var taskHoverOver = function() {
			$('#' + $(this).attr('id') + ' > .action-checklist').show();
		};
	
		var taskHoverOut = function() {
			$('#' + $(this).attr('id') + ' > .action-checklist').hide();
		};
		$('.task-item').hover(taskHoverOver, taskHoverOut);

	}

	/**
	* Binds an ajax request to mark the task as completed or still open to all check boxes
	*/
	function bindUpdateStatus() {
		$('.complete_task_box').click(function(){
			var id = $(this).val();
			var status = $(this).attr('checked') ? <?php echo TaskPeer::COMPLETED ?> : <?php echo TaskPeer::OPEN ?>;
			$.ajax({
				type: "POST",
				url: "<?php echo url_for('tasks/updateStatus')?>",
				data: "id=" + id + '&status=' + status,
				beforeSend: function(xhr) {
					$('#task_loading_' + id).show();		// display the loading image
					$('#status_update_' + id).hide();		// hide the status area
				},
				success: function(msg) {
					$('#task_loading_' + id).hide();		// display the loading image
					$('#status_update_' + id).show();		// hide the status area
					$('#status_update_' + id).html(msg);
				}
			});	
		});
	}
		

	// bind an ajax function to all check boxes when the document loads
	$(document).ready(function(){

		bindUpdateStatus();
		bindChecklistActions();	// bind all checklist actions

		bindEditTask();
		bindEditTaskList();
		bindAddTask();
		bindDeleteTaskList();
	});

</script>
